package com.yuexun.dhdoctor.module.contractmanage.ui;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;


import com.yuexun.dhdoctor.R;
import com.yuexun.dhdoctor.base.BaseEntity;
import com.yuexun.dhdoctor.base.BaseKJActivity;
import com.yuexun.dhdoctor.config.AppConfig;
import com.yuexun.dhdoctor.module.contractmanage.adapter.ContractManageTestAdapter;
import com.yuexun.dhdoctor.module.contractmanage.bean.ContractManageTestBean;
import com.yuexun.dhdoctor.module.contractmanage.model.impl.ContractManageModel;
import com.yuexun.dhdoctor.module.contractmanage.presenter.IContractManageTestPresenter;
import com.yuexun.dhdoctor.module.contractmanage.presenter.impl.ContractManageTestPresenter;
import com.yuexun.dhdoctor.module.contractmanage.ui.view.IContractManageTestView;
import com.yuexun.dhdoctor.widget.EmptyLayout;
import com.yuexun.dhdoctor.widget.PullToRefresh.FooterLoadingLayout;
import com.yuexun.dhdoctor.widget.PullToRefresh.PullToRefreshBase;
import com.yuexun.dhdoctor.widget.PullToRefresh.PullToRefreshList;

import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by work-jx on 2016/10/27.
 */
public class Act_ContractManageTest extends BaseKJActivity implements IContractManageTestView {
    @Bind(R.id.act_contractmanage_list)
    public PullToRefreshList actContractmanageList;
    @Bind(R.id.empty_layout)
    public EmptyLayout mEmptyLayout;
    @Bind(R.id.title_tv)
    TextView titleTv;

    ContractManageTestAdapter adapter;

    private ListView mList;
    IContractManageTestPresenter presenter;

    String healthid,name;
    List<ContractManageTestBean.RowsBean> rowsBeans = new ArrayList<>();

    private int nowpage = 1;
    private int pagesize = 20;

    @Override
    public void setRootView() {
        setContentView(R.layout.act_test);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        super.initData();
        listViewPreference();
        setTitle();
        healthid = getIntent().getExtras().getString("healthid");
        presenter = new ContractManageTestPresenter(this,new ContractManageModel());

        ApiForGetList();
    }

    //请求详情
    private void ApiForGetList() {
        Map<String, String> params = new HashMap<>();
        params.put("sessionUuid", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID));
        params.put("pageIndex", nowpage+"");
        params.put("pageSize", pagesize+"");
        params.put("cardNo", healthid);
        presenter.showContractManageTestList(params);
    }

    /**
     * 初始化ListView样式
     */
    private void listViewPreference() {
        mList = actContractmanageList.getRefreshView();
        mList.setDivider(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
        mList.setOverscrollFooter(null);
        mList.setOverscrollHeader(null);
        mList.setOverScrollMode(ListView.OVER_SCROLL_NEVER);
        actContractmanageList.setPullLoadEnabled(true);//设置上拉刷新功能关闭
        ((FooterLoadingLayout) actContractmanageList.getFooterLoadingLayout()).setNoMoreDataText("已经是最新状态");

        adapter = new ContractManageTestAdapter(mList, rowsBeans, R.layout.testlist_item);
        mList.setAdapter(adapter);

        actContractmanageList.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                nowpage = 1;
                ApiForGetList();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                nowpage = nowpage + 1;
                ApiForGetList();
            }
        });

        mEmptyLayout.setOnLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mEmptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
                nowpage = 1;
                ApiForGetList();
            }
        });

        mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Bundle bundle = new Bundle();
                bundle.putString("hosId",rowsBeans.get(position).getHosp_id());
                bundle.putString("rpeportNo",rowsBeans.get(position).getReport_no());
                bundle.putString("name",rowsBeans.get(position).getName());
                bundle.putString("reportname",rowsBeans.get(position).getItem_name());
                bundle.putString("reporttime",rowsBeans.get(position).getApply_dtime());
                Intent intent = new Intent(aty, Act_ContractManageTestDetail.class);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    public void setTitle() {
        titleTv.setText("化验报告单");
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @OnClick(R.id.back)
    public void onClick() {
       finish();
    }

    @Override
    public void showContractManageTestList(BaseEntity<ContractManageTestBean> contractManageTestBeanBaseEntity) {
        if (actContractmanageList.isPullRefreshEnabled()) {
            actContractmanageList.onPullDownRefreshComplete();
        }

        if (contractManageTestBeanBaseEntity.getStatus().equals("200")) {
            if (contractManageTestBeanBaseEntity.getDatas().size() == 0) {
                actContractmanageList.setHasMoreData(false);
            } else {
                if (actContractmanageList.isPullLoadEnabled()) {
                    actContractmanageList.onPullUpRefreshComplete();
                }
            }

            if (nowpage == 1) {
                rowsBeans.clear();
                rowsBeans = contractManageTestBeanBaseEntity.getDatas().get(0).getRows();
            } else {
                rowsBeans.addAll(contractManageTestBeanBaseEntity.getDatas().get(0).getRows());
            }
            adapter.refresh(rowsBeans);
        } else {
            ViewInject.toast(contractManageTestBeanBaseEntity.getMessage());
        }

        if (contractManageTestBeanBaseEntity.getDatas().size() == 0) {
            mEmptyLayout.setErrorType(EmptyLayout.NODATA);
        } else {
            mEmptyLayout.dismiss();
        }
    }
}
